package name.luojun.DBhelper;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import name.luojun.DBhelper.iface.UpgradeSupport;
public class SimpleMappedUpgradeSupport extends UpgradeSupport<Map<String, Object>> {
	private String rule;
	private String table;
	private String[] columns;
	private String[] primarykeys;
	public String[] getPrimarykeys() {
		return primarykeys;
	}
	public void setPrimarykeys(String[] primarykeys) {
		this.primarykeys = new String[primarykeys.length];
		for (int i = 0; i < primarykeys.length; i++) {
			this.primarykeys[i] = primarykeys[i].toUpperCase().trim();
		}
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table.toUpperCase();
	}
	public String[] getColumns() {
		return columns;
	}
	private String getAll;
	private String insert;
	private String modify;
	private String delete;
	private int insertcount = 0;
	private int deletecount = 0;
	private int modifycount = 0;
	public void setColumns(String[] columns) {
		this.columns = new String[columns.length];
		for (int i = 0; i < columns.length; i++) {
			this.columns[i] = columns[i].toUpperCase().trim();
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getAll() {
		return this.getJdbcTemplate().queryForList(getAll);
	}
	@Override
	public void insert(Map<String, Object> e) {
		insertcount++;
		this.getNamedParameterJdbcTemplate().update(insert, e);
	}
	@Override
	public void modify(Map<String, Object> e) {
		modifycount++;
		this.getNamedParameterJdbcTemplate().update(modify, e);
	}
	@Override
	public void delete(Map<String, Object> e) {
		deletecount++;
		this.getNamedParameterJdbcTemplate().update(delete, e);
	}
	@Override
	public void initDao() {
		try {
			if (primarykeys == null || primarykeys.length == 0) {
				throw new RuntimeException("property primarykeys should not be null!");
			}
			if (table == null || table.length() == 0) {
				throw new RuntimeException("property table should not be null!");
			}
			if (columns == null || columns.length == 0) {
				throw new RuntimeException("property columns should not be null!");
			}
			if (filePath == null || filePath.length() == 0) {
				throw new RuntimeException("property filePath should not be null!");
			}
			createSql();
			doUpgrade();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public void createSql() {
		this.getAll = "select * from " + table;
		StringBuffer insbuf = new StringBuffer();
		StringBuffer insv = new StringBuffer();
		StringBuffer modbuf = new StringBuffer();
		StringBuffer delbuf = new StringBuffer();
		insbuf.append("insert into " + table + " (");
		delbuf.append("delete from " + table + " where ");
		modbuf.append("update " + table + " set ");
		for (String str : columns) {
			insbuf.append(str + ",");
			insv.append(":" + str + ",");
			modbuf.append(str + "=:" + str + ",");
		}
		modbuf.deleteCharAt(modbuf.length() - 1);
		modbuf.append(" where ");
		for (String str : primarykeys) {
			insbuf.append(str + ",");
			insv.append(":" + str + ",");
			delbuf.append(str + "=:" + str + " and ");
			modbuf.append(str + "=:" + str + " and ");
		}
		insbuf.deleteCharAt(insbuf.length() - 1);
		insv.deleteCharAt(insv.length() - 1);
		insbuf.append(") values (");
		insbuf.append(insv + ")");
		delbuf.delete(delbuf.length() - 5, delbuf.length() - 1);
		modbuf.delete(modbuf.length() - 5, modbuf.length() - 1);
		this.insert = insbuf.toString();
		this.delete = delbuf.toString();
		this.modify = modbuf.toString();
	}
	@Override
	public String getRule() {
		return this.rule == null ? UpgradeManager.INSERTORUPDATE : this.rule;
	}
	public void setRule(String rule) {
		this.rule = rule;
	}
	@Override
	public String[] getPrimaryKey() {
		return primarykeys;
	}
	@Override
	public String[] getFields() {
		return columns;
	}
	@Override
	public void doUpgrade() {
		try {
			InputStream ins = getInputStream();
			if (ins == null) {
				log.error("upgrade resouce not found,upgrade stoped. filePath:" + this.filePath);
				return;
			}
			UpgradeManager.FileUpgradeForSimpleMap(this, Map.class, ins, "getAll", "insert", "modify", "delete", getPrimaryKey(),
					getFields(), getRule());
			deleteFile();
			log.info(table + " upgrade finished, insert " + insertcount + " records ,delete " + deletecount + " records ,update "
					+ modifycount + " records");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
